#include <iostream>
#include <fstream>
#include <algorithm>

#include "smartcar.hpp"
#include "student.hpp"

using namespace std;

int main() {
    // 读取学生信息
    ifstream student_info("../data/student_info.txt", ios::in);
    if (!student_info.is_open()) {
        cerr << "Failed to open student_info.txt" << endl;
        return 1;
    }
    vector<Student> students;
    string line;
    while (getline(student_info, line)) {
        int blank_pos = line.find(' ');
        if (blank_pos != string::npos) {
            Student student;
            student.set(line.substr(0, blank_pos), line.substr(blank_pos + 1));
            students.push_back(student);
        }
    }
    student_info.close();

    // 读取车辆信息
    ifstream car_info("../data/car_info.txt", ios::in);
    if (!car_info.is_open()) {
        cerr << "Failed to open car_info.txt" << endl;
        return 1;
    }
    vector<SmartCar> cars;
    while (getline(car_info, line)) {
        int blank_pos = line.find(' ');
        if (blank_pos != string::npos) {
            SmartCar car;
            car.set(line.substr(0, blank_pos));
            car.setID(line.substr(blank_pos + 1));
            cars.push_back(car);
        }
    }
    car_info.close();
    if (students.size() != cars.size()) {
        cerr << "The number of students and cars do not match!" << endl;
        return 1;
    }

    // 随机分配车辆给学生并保存到文件
    ofstream output("../data/assignment.txt", ios::out);
    if (!output.is_open()) {
        cerr << "Failed to open assignment.txt" << endl;
        return 1;
    }
    random_shuffle(students.begin(), students.end());
    random_shuffle(cars.begin(), cars.end());
    for (int i = 0; i < 10; ++i) {
        output << "Car ID: " << cars[i].get()[0] << ", Chassis ID: " << cars[i].get()[1];
        output << ", Student ID: " << students[i].get()[0] << ", Name: " << students[i].get()[1] << endl;
    }
    output.close();

    // 打印一辆车的信息并保存
    cars[0].print();
    cars[0].save("../data/car01_info.txt");

    // 浏览分配结果
    ifstream assignment("../data/assignment.txt", ios::in);
    if (!assignment.is_open()) {
        cerr << "Failed to open assignment.txt" << endl;
        return 1;
    }
    vector<string> assignments;
    while (getline(assignment, line)) {
        assignments.push_back(line);
    }
    assignment.close();
    int current_index = 0;
    char command;
    cout << "Press 'n' for next, 'p' for previous, 'q' to quit:" << endl;
    cout << "==================================================" << endl;
    cout << assignments[current_index] << " ";
    while (true) {
        cin >> command;
        if (command == 'n') {   // 查看下一项
            if (current_index < assignments.size() - 1) {
                current_index++;
                cout << assignments[current_index] << " ";
            } else {
                cout << "This is the last car." << " ";
            }
        } else if (command == 'p') {    // 查看上一项
            if (current_index > 0) {
                current_index--;
                cout << assignments[current_index] << " ";
            } else {
                cout << "This is the first car." << " ";
            }
        } else if (command == 'q') {    // 退出
            break;
        } else {    // 无效命令
            cout << "Invalid command." << " ";
        }
    }
}